package com.cskaoyan.config;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.LogService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @author Fantasylkh
 */
@Configuration
@Aspect
public class LogAspectJConfig {

    @Autowired
    LogService logService;

    @Pointcut("@annotation(com.cskaoyan.config.Logging)")
    public void logPointcut() {
    }

    @AfterReturning(value = "execution(* com.cskaoyan.controller.AuthController.login(..))", returning = "result")
    public void afterReturningLogin(Object result) {
        if (result instanceof BaseRespVo) {
            BaseRespVo baseRespVo = (BaseRespVo) result;
            logService.login(baseRespVo);
        }
    }

    @AfterReturning(value = "logPointcut()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        if (result instanceof BaseRespVo) {
            BaseRespVo baseRespVo = (BaseRespVo) result;
            if (baseRespVo.getErrno() == 0) {
                logService.loggin(baseRespVo, joinPoint);
            }
        }
    }

    @AfterThrowing(value = "logPointcut()", throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint, Exception exception) {
        logService.exception(joinPoint, exception);

    }


}
